package com.sanjiahantang.youjiarole.module.me.require.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.popuwindow.SelectPhotoPopupwindow;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.common.views.impl.MyProjectpartnerActivity;
import com.sanjiahantang.youjiarole.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiarole.module.me.order.views.impl.ServiceOrderActivity;
import com.sanjiahantang.youjiarole.module.me.quotation.views.impl.QuotationActivity;
import com.sanjiahantang.youjiarole.module.me.require.beans.RequireDetailInitResponseBean;
import com.sanjiahantang.youjiarole.module.me.require.presenters.RequirePresenter;
import com.sanjiahantang.youjiarole.module.me.require.views.RequireView;
import com.sanjiahantang.youjiarole.utils.SPUtils;

/**
 * Created by zy on 2017/10/24.
 */

public class RequireDetailActivity extends BaseMvpActivity<RequireView, RequirePresenter> implements RequireView, View.OnClickListener {

    private RequireDetailInitResponseBean.Data data;
    private int takeOrderState = -1;
    private int roleType = -1;
    private int id;
    private View btnTakeOrder;
    private TextView btnOffer;
    private int designerState;
    private ImageView ivPhone;
    private SelectPhotoPopupwindow photoPopupwindow;
    private String currentRoleId,currentPhone;
    private View rootView;
    private RequireOrderBean.Data orderData;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public RequirePresenter initPresenter() {
        return new RequirePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_require);
        rootView = findViewById(R.id.rootView);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("装修需求单");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        btnTakeOrder = findViewById(R.id.btnTakeOrder);
        btnOffer = (TextView) findViewById(R.id.btnOffer);
        ivPhone = (ImageView) findViewById(R.id.ivPhone);
        ivPhone.setOnClickListener(this);

        btnTakeOrder.setOnClickListener(this);
        btnOffer.setOnClickListener(this);

        takeOrderState = getIntent().getIntExtra("state", -1);
        roleType = getIntent().getIntExtra("type", -1);

        if (takeOrderState < 0 || roleType < 0) {
            findViewById(R.id.panelOffer).setVisibility(View.GONE);
        } else if (takeOrderState == 2) {
            findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
            btnTakeOrder.setVisibility(View.VISIBLE);
            btnOffer.setVisibility(View.GONE);
        } else if (takeOrderState == 1 && roleType == 0) {
            findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
            btnTakeOrder.setVisibility(View.GONE);
            btnOffer.setVisibility(View.VISIBLE);
            btnOffer.setText("设计报价");
        } else if (takeOrderState == 1 && roleType == 1) {
            findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
            btnTakeOrder.setVisibility(View.GONE);
            btnOffer.setVisibility(View.VISIBLE);
            btnOffer.setText("推荐合伙人");
        }

        photoPopupwindow = new SelectPhotoPopupwindow($this, this);
        photoPopupwindow.renameButtons("分包给其他人设计","自己设计");
        currentRoleId = SPUtils.get($this, SPConfig.ROLE_ID,"").toString();
        currentPhone = SPUtils.get($this, SPConfig.PHONE,"").toString();


    }

    @Override
    public void initData() {
        id = getIntent().getIntExtra(KEY_ID, 0);
        presenter.indexUserRequireInfoForRole($this, id, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                data = (RequireDetailInitResponseBean.Data) content;
                TextView txtName = (TextView) findViewById(R.id.txtName);
                txtName.setText("1".equals(data.getSex()) ? data.getUserName().substring(0,1) + " 女士" : data.getUserName().substring(0,1) + " 先生");


                TextView txtIdCode = (TextView) findViewById(R.id.txtIdCode);
                if (!TextUtils.isEmpty(data.getIdCode())) {
                    txtIdCode.setText(data.getIdCode());
                }
//                TextView txtSex = (TextView) findViewById(R.id.txtSex);
//                txtSex.setText("1".equals(data.getSex()) ? "女" : "男");
                TextView txtJob = (TextView) findViewById(R.id.txtJob);
                txtJob.setText(data.getJobName());
                TextView txtCity = (TextView) findViewById(R.id.txtCity);
                txtCity.setText(data.getAddress());
                TextView txtAddress = (TextView) findViewById(R.id.txtAddress);
                txtAddress.setText(data.getApartmentName());
                TextView txtBaby = (TextView) findViewById(R.id.txtBaby);
                txtBaby.setText(data.getIsHaveBaby() == 0 ? "否" : "是");
                TextView txtOld = (TextView) findViewById(R.id.txtOld);
                txtOld.setText(data.getHasTogether() == 0 ? "否" : "是");
                TextView txtRoomType = (TextView) findViewById(R.id.txtDoor);
                txtRoomType.setText(data.getApartmentTypeName());
                TextView txtStyle = (TextView) findViewById(R.id.txtStyle);
                txtStyle.setText(data.getLikeStyleName());
                TextView txtArea = (TextView) findViewById(R.id.txtArea);
                txtArea.setText(data.getApartmentArea());
                TextView txtPrice = (TextView) findViewById(R.id.txtPrice);
                txtPrice.setText(data.getBudgetName());
                TextView txtPreTime = (TextView) findViewById(R.id.txtPreTime);
                txtPreTime.setText(data.getPlanDecorateTime());

                designerState = data.getDesignStateId();

                if(roleType == 0 && data.getIsDesignFree() == 1){
                    findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                    btnTakeOrder.setVisibility(View.GONE);
                    btnOffer.setVisibility(View.VISIBLE);
                    btnOffer.setBackgroundResource(R.drawable.x_shape_dark_gray_r3);
                    btnOffer.setText("此设计免费");
                    btnOffer.setOnClickListener(null);
                }else if (data.getIsCommitOrder() == 1 && roleType == 0){
                    findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                    btnTakeOrder.setVisibility(View.GONE);
                    btnOffer.setVisibility(View.VISIBLE);
                    btnOffer.setBackgroundResource(R.drawable.x_shape_dark_gray_r3);
                    btnOffer.setText("查看设计报价");
                    btnOffer.setOnClickListener(null);

                    presenter.getRequireOrderPrice($this, data.getId(), new OnLoadListener() {
                        @Override
                        public void onLoad(final Object content) {
                            orderData = (RequireOrderBean.Data) content;

                            findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                            btnTakeOrder.setVisibility(View.GONE);
                            btnOffer.setVisibility(View.VISIBLE);
                            btnOffer.setText("查看设计报价");
                            btnOffer.setBackgroundResource(R.drawable.x_shape_green);
                            btnOffer.setOnClickListener(RequireDetailActivity.this);
                        }
                    });
                }else if(roleType == 0 && data.getDesignStateId() != 0){

                    if (data.getIsSubcontract() == 1 && currentPhone.equals(data.getDesignRoleId())){
                        //分包项目 分包人
                        findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                        btnTakeOrder.setVisibility(View.GONE);
                        btnOffer.setVisibility(View.VISIBLE);
                        btnOffer.setBackgroundResource(R.drawable.x_shape_dark_gray_r3);
                        btnOffer.setText("查看设计报价");
                        btnOffer.setOnClickListener(null);
                    }else if (data.getIsSubcontract() == 1 && !currentPhone.equals(data.getDesignRoleId())){
                        //分包项目 被分包人
                        findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                        btnTakeOrder.setVisibility(View.GONE);
                        btnOffer.setVisibility(View.VISIBLE);
                        btnOffer.setText("设计报价");
                    }else{
                        findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                        btnTakeOrder.setVisibility(View.GONE);
                        btnOffer.setVisibility(View.VISIBLE);
                        btnOffer.setText("设计报价");
                    }

                }else if(roleType == 1 && data.getProjectStateId() != 0){
                    if (data.getProjectRecommend() == 0){
                        findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                        btnTakeOrder.setVisibility(View.GONE);
                        btnOffer.setVisibility(View.VISIBLE);
                        btnOffer.setText("推荐合伙人");
                    }else{
                        findViewById(R.id.panelOffer).setVisibility(View.GONE);
                    }
                }

                if (roleType == 0 && data.getIsBidProject() == 1 && data.getIsBidProjectState() < 2){
                    findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                    btnTakeOrder.setVisibility(View.GONE);
                    btnOffer.setVisibility(View.VISIBLE);
                    btnOffer.setText("投标");
                    takeOrderState = 3;

                    ivPhone.setImageResource(R.drawable.i_phone_white_strike);
                    ivPhone.setBackgroundResource(R.drawable.x_shape_dark_gray_r3);
                    ivPhone.setOnClickListener(null);
                }

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.ivPhone:
                if (data.getPhone() != null) {
                    Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getPhone()));
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent1);
                }
                break;
            case R.id.btnTakeOrder:
                String prompting = getResources().getString(R.string.info_take_order_project_manager);

                if (roleType == 1 && designerState == 0){
                    prompting = getResources().getString(R.string.info_take_order_project_manager_reverse);
                }else if(roleType == 0){
                    prompting = getResources().getString(R.string.info_take_order);
                }

                Utils.showDialog($this, "提示", prompting, "", "", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utils.dismissDialog();
                        switch (view.getId()) {
                            case R.id.txtDialogSure:
                                presenter.fastBindUserCustomer($this, id, roleType, data, new OnLoadListener() {
                                    @Override
                                    public void onLoad(Object content) {
                                        Utils.showToast($this,"接单成功");
                                        if (roleType == 0){
                                            if (currentRoleId.contains("3")){
                                                initData();
                                            }else{
                                                toDesignPricePage();
                                            }

                                        }else if (roleType == 1){
                                            //项目经理
                                            ((Activity)$this).finish();
                                        }else if (roleType == 2){
                                            //监理
                                            ((Activity)$this).finish();
                                        }
                                    }
                                });
                                break;
                            case R.id.txtDialogCancel:
                                break;
                        }
                    }
                });
                break;
            case R.id.btnOffer:
                if (takeOrderState == 3){
                    Utils.showDialog($this, "提示", getResources().getString(R.string.info_take_order_bid), "", "", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Utils.dismissDialog();
                            switch (view.getId()) {
                                case R.id.txtDialogSure:
                                    //投标
                                    presenter.saveBidRequireInfoForRole($this,id, new OnLoadListener() {
                                        @Override
                                        public void onLoad(Object content) {
                                            Utils.showToast($this,"投标成功");
                                            finish();
                                        }
                                    });

                                    break;
                                case R.id.txtDialogCancel:
                                    break;
                            }
                        }
                    });
                }else{
                    if (roleType == 0) {
                        if(data.getIsCommitOrder() == 1){
                            toOrderPage();
                        }else if(data.getIsSubcontract() == 1 && currentPhone.equals(data.getDesignPhone())){
                            toDesignPricePage();
                        }else if (currentRoleId.contains("3") && designerState == 1){
                            showBottomDialog();
                        }else{
                            toDesignPricePage();
                        }
                    }

                    if (roleType == 1) {
                        Intent intent = new Intent($this, MyProjectpartnerActivity.class);
                        intent.putExtra(KEY_TYPE, 1);
                        startActivityForResult(intent, 2);
                    }
                }


                break;
            case R.id.btn_take_photo:
                recommendSubPackagePartner();
                photoPopupwindow.dismiss();
                break;
            case R.id.btn_pick_photo:
                toDesignPricePage();
                photoPopupwindow.dismiss();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            finish();
        }
        if (requestCode == 2 && resultCode == RESULT_OK) {
            UserOBean.Data beanData = (UserOBean.Data) data.getSerializableExtra(KEY_PARAM);
            presenter.updateRequireDesign($this, id, beanData, new OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    if (content instanceof Object[]) {
                        Object[] p = (Object[]) content;
                        switch ((int) p[0]) {
                            case CALLBACK_TYPE_FAILURE:
                                toOrderPage();
                                break;
                            case CALLBACK_TYPE_RECOVER:
                                Utils.showToast($this, "推荐成功");
                                finish();

                                break;
                        }
                    }
                }
            });
        }
        if (requestCode == 3 && resultCode == RESULT_OK){
            UserOBean.Data beanData = (UserOBean.Data) data.getSerializableExtra(KEY_PARAM);
            presenter.addRequireSubcontractInfo($this, id, beanData, new OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    if (content instanceof Object[]) {
                        Object[] p = (Object[]) content;
                        switch ((int) p[0]) {
                            case CALLBACK_TYPE_FAILURE:
                                Utils.showToast($this, "推荐失败");
                                break;
                            case CALLBACK_TYPE_RECOVER:
                                Utils.showToast($this, "推荐成功");
                                finish();

                                break;
                        }
                    }
                }
            });
        }
    }

    private void toDesignPricePage(){
        //设计师
//        Intent intent = new Intent($this, QuotationActivity.class);
//        intent.putExtra(KEY_ID,id);
//        intent.putExtra(KEY_PARAM,data);
//        $this.startActivity(intent);
//        ((Activity)$this).finish();
        Intent intent = new Intent($this, QuotationActivity.class);
        intent.putExtra(KEY_ID, id);
        intent.putExtra(KEY_PARAM, data);
        startActivityForResult(intent, 1);
    }

    private void showBottomDialog(){
        photoPopupwindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    private void recommendSubPackagePartner(){
        Intent intent = new Intent($this, MyProjectpartnerActivity.class);
        intent.putExtra(KEY_TYPE, 4);
        startActivityForResult(intent, 3);
    }

    private void toOrderPage(){
        if (orderData != null && !TextUtils.isEmpty(orderData.getOrderCode())){
            Intent intent = new Intent($this, RequireOrderDetailActivity.class);
            intent.putExtra(KEY_PARAM, orderData);
            startActivity(intent);
        }

    }
}
